<?php
namespace app\order\controller;
use app\rbac\model\AdminM;
use app\order\model\OrderM;
use think\Db;
use app\base\controller\Base;

class Order extends Base
{
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst()
    {
        $model = new OrderM();
        $data = $model->lst(15);
//        var_dump($data);die;
        $this->assign(array(
            '_page_title'=>'订单列表',
            '_page_title_english'=>'Order list',
            '_btn_title'=>'订单列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>' iconfa-bell',
            'data'=>$data,
        ));
        return $this->fetch();
    }

    /**
     * 发货
     * @return \think\response\Json
     */
    public function cancle()
    {
        $order_id = $this->request->post('order_id');
        $model = new OrderM();
        $model->cancle($order_id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }

    /**
     * 发货
     * @return mixed
     */
    public function fh($order_id)
    {
        //先判断权限
        $admin_id = session('admin_id');

        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new OrderM();
            $res = $model->fh($data);
            if($res)
            {
                $this->success('发货完成',url('order/Order/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        $year = date('Y',time());
        //物流类型
        $lData = Db::table('order_logistic_type')->where('is_usable',1)->field('id,name')->select();
        //收货信息
        $data = Db::table('order_order_'.$year)->where('order_id',$order_id)->field('address')->find();
        $_address = json_decode($data['address'],true);
        $_in = $_address['province_id'].','.$_address['city_id'].','.$_address['qu_id'];
        $city = Db::table('city')->where('id','in',$_in)->column('name','id');
        $_address['province_name'] = $city[$_address['province_id']];
        $_address['city_name'] = $city[$_address['city_id']];
        $_address['qu_name'] = $city[$_address['qu_id']];

        $this->assign(array(
            '_page_title'=>'订单发货',
            '_page_title_english'=>'order fh',
            '_btn_title'=>'订单列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-bell',
            'order_id'=>$order_id,
            'lData'=>$lData,
            'address'=>$_address,
        ));
        return $this->fetch();
    }

    /**
     * 查看物流
     * @return \think\response\Json
     */
    public function kdsearch($order_id)
    {
        $model = new OrderM();
        $data = $model->kdsearch($order_id);
//        var_dump($data);die;
        $this->assign(array(
            '_page_title'=>'查看物流',
            '_page_title_english'=>'order wl',
            '_btn_title'=>'订单列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-bell',
            'order_id'=>$order_id,
            'data'=>$data,
        ));
        return $this->fetch();
    }






}
